/**
 * Created by chengming on 18/5/27.
 * version 1.0
 */
const BaseJsonAdapter = require('../base/BaseJsonAdapter');
const ArticleHrefAdapter = require('../ArticleHrefAdapter');
const iconv = require("iconv-lite");
const vm = require('vm');
const request = require('request-promise');
const logger = require('../../conf/logger');
const querystring = require("querystring")
const cheerio = require('cheerio');
const urlTool = require('url');

/**
 * 北京互金网
 * @type {module.Bjp2p}
 */
module.exports = class NingboGovBre extends ArticleHrefAdapter {

    initWinEnv(){
        global.window = {
            innerWidth:1232,
            innerHeight:632,
            screenX:11,
            screenY:22,
            screen:{
                width:1280,
                height:800
            }
        };

        global.document = {
            documentElement:{
                clientWidth:1217,
                clientHeight:550
            },
            body:{
                clientWidth:1217,
                clientHeight:1467
            },
            _cookie: [],

            get cookie(){
                return this._cookie;
            },

            set cookie(val){
                this._cookie.push(val);
            }
        };
    }

    async doRequest(){

        //初始化窗口属性
        this.initWinEnv();

        //创建全局cookie
        var jar = request.jar();

        //使用cookie
        const body = await super.doRequest();
        return body;
    }

    async execute(){
        logger.debug('开始执行抓取->'+this.pageName);

        //获取请求结果
        let body = await this.doRequest();

        //根据指定encoding解析页面
        let decodeBody = iconv.decode(body, this.encoding).toString();
        decodeBody = decodeBody.substring(decodeBody.indexOf('<recordset>')+'<recordset>'.length,decodeBody.indexOf('</recordset>'));
        decodeBody = decodeBody.replace(/<\!\[CDATA\[/g,'');
        decodeBody = decodeBody.replace(/]]>/g,'');
        decodeBody = decodeBody.replace(/<record>/g,'');
        decodeBody = decodeBody.replace(/<\/record>/g,'');
        /*if(this.pageSite == 'https://ambcrypto.com/category/news/') {
            let startIndex = decodeBody.indexOf('<div class="mvp-blog-story-text left relative"');
            await report(this.pageSite, this.pageTitle, '', '请求结束,decodeBody: ' + decodeBody.substr(startIndex));
        }*/
        logger.debug(this.pageName+'抓取执行完毕,开始分析文本');

        if(decodeBody.trim().length<5) {
            logger.warn(this.pageName+'抓取到的页面为空');
            if(this.isTest == false){
                await report(this.pageSite, this.pageTitle, '', '抓取到的页面为空');
            }
            return {
                code: 401,
                errMsg: '抓取到的页面为空'
            };
        }

        if(!this.linkRule || this.linkRule.length == 0){
            logger.warn(this.pageName+'没有链接抓取规则');
            if(this.isTest == false){
                await report(this.pageSite, this.pageTitle, '', '没有链接抓取规则');
            }
            return {
                code: 401,
                errMsg: '没有链接抓取规则'
            };
        }

        let contentBlock = this.parseHtml(decodeBody, this.linkRule);
        if(contentBlock.rangeText.length == 0) {
            logger.warn(this.pageName + ">>>" + this.linkRule + "此抓取规则下,无内容匹配，decodeBody：" + decodeBody);
            if (this.isTest == false) {
                await report(this.pageSite, this.pageTitle, '', this.linkRule + "此抓取规则下,无内容匹配, decodeBody: " + decodeBody);
            }
            return {
                code: 402,
                errMsg: this.linkRule + "此抓取规则下,无内容匹配" + decodeBody
            };
        } else {
            logger.info(this.pageName + ">>>>>>res: " + contentBlock.rangeText);
        }

        await super.postSocket(this.id, this.rangeMd5, contentBlock.rangeText, contentBlock.contents,  decodeBody, this.cates, this.pageTitle, this.sourceTime, this.channel,
            this.followLink, this.foreign, this.contentRule, this.linkUniqueBy);

        return await super.postFilterUnique(contentBlock.contents,
            this.cates,
            this.foreign,
            this.linkUniqueBy,
            this.followLink,
            this.sourceTime,
            this.pageTitle,
            this.titleRule,
            this.contentRule,
            this.channel);
    }

}
